package com.mcc.system.service.impl;

import java.util.List;
import javax.annotation.PostConstruct;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mcc.common.core.domain.entity.SysDictData;
import com.mcc.common.core.domain.entity.SysDictType;
import com.mcc.common.exception.CustomException;
import com.mcc.common.utils.DictUtils;
import com.mcc.common.utils.StringUtils;
import com.mcc.system.mapper.SysDictDataMapper;
import com.mcc.system.mapper.SysDictTypeMapper;
import com.mcc.system.service.ISysDictTypeService;

/**
 * 字典 业务层处理
 *
 * @author gxy
 */
@Service
public class SysDictTypeServiceImpl implements ISysDictTypeService {

    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init() {
        List<SysDictType> dictTypeList = dictTypeMapper.selectList(null);
        for (SysDictType dictType : dictTypeList) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("dict_type", dictType.getDictType());
            List<SysDictData> dictDatas = dictDataMapper.selectList(queryWrapper);
            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
        }
    }

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeList(SysDictType dictType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(dictType)) {
            queryWrapper.like(ObjectUtil.isNotEmpty(dictType.getDictName()), "dict_name", dictType.getDictName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(dictType.getStatus()), "status", dictType.getStatus());
            queryWrapper.like(ObjectUtil.isNotEmpty(dictType.getDictType()), "dict_type", dictType.getDictType());
            queryWrapper.ge(ObjectUtil.isNotEmpty(dictType.getBeginTime()), "date_format(create_time,'%Y-%m-%d')", dictType.getBeginTime());
            queryWrapper.le(ObjectUtil.isNotEmpty(dictType.getEndTime()), "date_format(create_time,'%Y-%m-%d')", dictType.getEndTime());
        }
        return dictTypeMapper.selectList(queryWrapper);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_type", dictType);
        dictDatas = dictDataMapper.selectList(queryWrapper);
        if (StringUtils.isNotEmpty(dictDatas)) {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return dictTypeMapper.selectById(dictId);
    }

    /**
     * 批量删除字典类型信息
     *
     * @param dictIds 需要删除的字典ID
     * @return 结果
     */
    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        for (Long dictId : dictIds) {
            SysDictType dictType = selectDictTypeById(dictId);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("dict_type", dictType.getDictType());
            if (dictDataMapper.selectCount(queryWrapper) > 0) {
                throw new CustomException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
        }
        if (dictIds.length == 0) return 0;
        int count = dictTypeMapper.deleteBatchIds(ListUtil.toList(dictIds));
        if (count > 0) {
            DictUtils.clearDictCache();
        }
        return count;
    }

    /**
     * 清空缓存数据
     */
    @Override
    public void clearCache() {
        DictUtils.clearDictCache();
    }

    /**
     * 新增保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    public int insertDictType(SysDictType dictType) {
        int row = dictTypeMapper.insert(dictType);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 修改保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDictType(SysDictType dictType) {
        SysDictType oldDict = dictTypeMapper.selectById(dictType.getDictId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_type", oldDict.getDictType());
        SysDictData dictData = new SysDictData();
        dictData.setDictType(dictType.getDictType());
        dictDataMapper.update(dictData, queryWrapper);
        int row = dictTypeMapper.updateById(dictType);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public boolean checkDictTypeUnique(SysDictType dict) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_type", dict.getDictType());
        queryWrapper.ne(ObjectUtil.isNotEmpty(dict.getDictId()), "dict_id", dict.getDictId());
        return dictTypeMapper.selectCount(queryWrapper) == 0;
    }
}
